import {Component, OnInit} from '@angular/core';
import {ModalController, NavParams} from '@ionic/angular';
import {AppGlobalService} from '../../shared/service/app-global.service';
import {HttpService} from '../../shared/service/http.service';
import {FormGroup, FormControl, Validators, FormGroupDirective} from '@angular/forms';
import { isString } from 'util';
import { first } from 'rxjs/operators';

@Component({
    selector: 'app-industry-modal',
    templateUrl: './industry-modal.component.html',
    styleUrls: ['./industry-modal.component.scss']
})
export class IndustryModalComponent implements OnInit {

    data: any;
    industryList: any;
    list: any = [];
    bigIndus: any = {background: false};
    selectIndus: any = [];
    checkedIndus: any = "";
    valueFromparent: any;
    modalForm: FormGroup = new FormGroup({});
    queryType: any;

    constructor(private modalCtrl: ModalController,
                private navParams: NavParams,
                private appGlobalService: AppGlobalService,
                private http: HttpService) {
    }

    ngOnInit() {
        this.data = this.navParams.data.value;
        this.queryType = this.data.item.table.queryType;
        console.log(this.data, 'data');
        let checkedValue;
        if (!this.data.item.value || !this.data.item.value.length) {
            checkedValue = [];
        } else if (this.data.item.value && this.data.item.value.indexOf('#') == 0 ) {
            checkedValue = [];
        } else if (isString(this.data.item.value)) {
            checkedValue = this.data.item.value.split(',');
        } else {
            checkedValue = this.data.item.value;
        }
        const table = {
            table: this.data.item.table
        };
        if (this.data.city && this.queryType === 'district') { table.table.condition[0].valueStr = this.data.city; }
        const params = {
            content: table,
            id: this.data.id,
            sessionId: localStorage.getItem('sessionId')
        };
        // 请求获取新数据
        this.http.post('application/submit', params, res => {
            // 请求数据成功
            if (res.data.data) {
                this.list = res.data.data;
                this.list.forEach(element => {

                    element.checkLen = 0;
                    if (element.children) {
                        element.children.forEach(ele => {
                            ele.checkLen = 0;
                            if (checkedValue.length) {
                                checkedValue.forEach(e => {
                                    // 获取已选的值
                                    if (ele.code == e) {
                                        element.checkLen += 1;
                                        ele.selected = true;
                                        this.selectIndus.push(ele);
                                    }
                                });
                            }
                        });
                    } else {
                        if (checkedValue.length) {
                            checkedValue.forEach(e => {
                                // 获取已选的值
                                if (element.code == e) {
                                    element.checkLen += 1;
                                    element.selected = true;
                                    this.selectIndus.push(element);
                                }
                            });
                        }
                    }

                });
                this.getSmallIndus(this.list[0], first);
            } else {
                // 请求数据失败
                this.appGlobalService.showToast(res.message, 2000, 'middle');
            }
        });
    }

    // 点击大行业，渲染小行业
    getSmallIndus(big, first?) {
        console.log(this.list, 'list');
        this.bigIndus.background = false;
        this.bigIndus = big;
        this.bigIndus.background = true;
        if (big.label === '全国' && !first) {
            big.selected = !big.selected;
            if (big.selected) {
                // 清除省份及城市
                this.list.forEach(bigindus => {
                    bigindus.checkLen = 0;
                    if (bigindus.children) {
                        bigindus.children.forEach(element => {
                            element.selected = false;
                        });
                    }

                });
                this.selectIndus = [];
                // 选中全国
                big.checkLen = 1;
                this.selectIndus.push(big);
            } else {
                this.selectIndus = [];
                big.checkLen = 0;
            }
          }
        if (big.code.length === 4 && !first) {
            big.selected = !big.selected;
            if (big.selected) {
                // 清除大区及小商区
                this.list.forEach(bigindus => {
                    bigindus.checkLen = 0;
                    if (bigindus.children) {
                        bigindus.children.forEach(element => {
                            element.selected = false;
                        });
                    }

                });
                this.selectIndus = [];
                // 选中全市
                big.checkLen = 1;
                this.selectIndus.push(big);
                // 清除全国
                if (this.list[0].label === '全国') {
                    this.list[0].checklen = 0;
                }

            } else {
                this.selectIndus = [];
                big.checkLen = 0;
            }
          }
    }

    // 选中小行业
    selectSmallIndus(bigindus, item) {
          // 清除全国 全市
  if (this.list[0].label === '全国' || this.list[0].label === '全市') {
    this.list[0].checkLen = 0;
    this.list[0].selected = false;
    const index = this.selectIndus.findIndex(v => (v.label === '全国' || v.label === '全市'));
    if (index > -1) {
        this.selectIndus.splice(index, 1);
     }
    }
  item.selected = !item.selected;
  if (item.selected) {
            // tslint:disable-next-line:max-line-length
            if ((item.code.length === 2) || (item.code.length === 6 )) {
                bigindus.children.forEach(element => {
                    if (element.selected) {
                        const index = this.selectIndus.findIndex(v => v.code === element.code);
                        if (index > -1) { this.selectIndus.splice(index, 1); }
                        element.selected = false;
                    }
                });
                bigindus.checkLen = 0;
                this.checkNum(item, bigindus);
                console.log("dddddddddd");
            } else {
              //   不包含全市 全行业
                if (!bigindus.children[0].selected || bigindus.children[0].code.length > bigindus.code.length) {
                    this.checkNum(item, bigindus);
                    console.log("dddddddddd333");
                } else {
                    const index = this.selectIndus.findIndex(v => v.code === bigindus.children[0].code);
                    this.selectIndus.splice(index, 1);
                    bigindus.children[0].selected = false;
                    bigindus.checkLen -= 1;
                    this.checkNum(item, bigindus);
                    console.log("dddddddddd4444");
                }
            }
    } else {
        const index = this.selectIndus.findIndex(v => v.code === item.code);
        this.selectIndus.splice(index, 1);
        bigindus.checkLen -= 1;
        if (bigindus.checkLen < 0) {
          bigindus.checkLen = 0;
        }
    }
    }

    checkNum(item, bigindus) {
        if (this.selectIndus.length > 9) {
            this.appGlobalService.showToast('不能超过10个', 2000, 'top');
            item.selected = false;
            return
        } else {
            item.selected = true;
            this.selectIndus.push(item);
            bigindus.checkLen += 1;
        }
    }

    dismiss() {
        this.modalCtrl.dismiss(this.selectIndus);
    }

    cancel() {
        this.modalCtrl.dismiss();
    }
}
